<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="168px">
      <el-form-item label="仓库" prop="scId">
        <el-select v-model="queryParams.scId" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in ck" :key="index" :label="item.name"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类目" prop="productType">
        <el-select v-model="queryParams.productType" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in category" :key="index" :label="item.name"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品品牌" prop="productBrand">
        <el-select v-model="queryParams.productBrand" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in brand" :key="index" :label="item.name"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:stock:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange" :row-class-name="Index">
      <el-table-column label="序号" align="center" prop="index" />
      <el-table-column label="仓库名称" align="center" prop="scName" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="商品类目" align="center" prop="productType" />
      <el-table-column label="商品品牌" align="center" prop="productBrand" />
      <el-table-column label="库存数量" align="center" prop="stockNum" />
      <el-table-column label="含税价格" align="center" prop="taxPrice" >
        <template slot-scope="scope">
          <span>{{ Number(scope.row.taxPrice).toLocaleString() }}</span>
        </template>
      </el-table-column>
      <el-table-column label="含税金额" align="center" prop="taxAmount" >
        <template slot-scope="scope">
          <span>{{ Number(scope.row.taxAmount).toLocaleString() }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listStock, getStock, delStock, addStock, updateStock, getCategory, getBrand } from "@/api/system/stock";
import { center } from "@/api/system/order";

export default {
  name: "Stock",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品库存表格数据
      stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scId: null,
        scName: null,
        productId: null,
        productCode: null,
        productName: null,
        productType: null,
        productBrand: null,
      },
      // 表单参数
      form: {},
      // 仓库下拉框
      ck: [],
      // 商品类目
      category: [],
      // 商品品牌
      brand: [],
      // 表单校验
      rules: {
        scId: [
          { required: true, message: "仓库ID不能为空", trigger: "blur" }
        ],
        productId: [
          { required: true, message: "商品ID不能为空", trigger: "blur" }
        ],
        stockNum: [
          { required: true, message: "库存数量不能为空", trigger: "blur" }
        ],
        taxPrice: [
          { required: true, message: "含税价格不能为空", trigger: "blur" }
        ],
        taxAmount: [
          { required: true, message: "含税金额不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品库存列表 */
    getList() {
      this.loading = true;
      listStock(this.queryParams).then(response => {
        this.stockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      center({categoryType:1}).then(response => {
        this.ck = response.rows;
        console.log(this.ck);
      });
      getCategory().then(response => {
        this.category = response.rows;
        console.log(this.category);
      });
      getBrand().then(response => {
        this.brand = response.rows;
        console.log(this.brand);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //序号
    Index({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        scId: null,
        productId: null,
        stockNum: null,
        taxPrice: null,
        taxAmount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品库存";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStock(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品库存";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStock(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStock(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品库存编号为"' + ids + '"的数据项？').then(function() {
        return delStock(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/stock/export', {
        ...this.queryParams
      }, `stock_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
